package edu.sysu.cshare.gui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import edu.sysu.cshare.control.UserManager;
import edu.sysu.cshare.model.Course;
import edu.sysu.cshare.model.User;
import edu.sysu.cshare.util.gui.GUI;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.awt.Color;

public class Dlg_UserDetail extends JDialog {

	private final JPanel contentPanel = new JPanel();
	private JTextField textField;
	private JLabel lblId;
	private JLabel lblClass;
	private JLabel lblRole;
	private JTextField tfDesc;
	private UserManager userManager;
	private User user ;
	private JLabel lblCourseInfo;

	/**
	 * Launch the application.
	 */
	
	
	public static void main(String[] args) {
		try {
			UserManager userManager = new UserManager(UserManager.getUserById("08382048"));
			Dlg_UserDetail dialog = new Dlg_UserDetail(userManager);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void  updateValue(){
		lblId.setText(String.format("ID:%s  姓名:%s",user.getId(),user.getName()));
		lblClass.setText("班级:"+user.getClazz().getName());
		lblRole.setText(String.format("权限:%d", user.getRole()));
		textField.setText(user.getNickname());
		tfDesc.setText(user.getDescrip());
		StringBuilder sb = new StringBuilder();
		for(Course c : user.getCourses()){
			sb.append(c.getName()+" ");
		}
		lblCourseInfo.setText("已选课程:"+sb);
	}
	private void saveChange(){
		user.setDescrip(tfDesc.getText());
		user.setNickname(textField.getText());
		userManager.merge(user);
	}
	/**
	 * Create the dialog.
	 */
	public Dlg_UserDetail(UserManager userManager) {
		this.userManager = userManager;
		this.user=userManager.getActor();
		setBounds(100, 100, 450, 300);
		setLocationRelativeTo(null);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBackground(new Color(211, 211, 211));
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		GridBagLayout gbl_contentPanel = new GridBagLayout();
		gbl_contentPanel.columnWidths = new int[]{46, 120, 0, 220, 0};
		gbl_contentPanel.rowHeights = new int[]{26, 14, 20, 14, 0};
		gbl_contentPanel.columnWeights = new double[]{0.0, 0.0, 0.0, 1.0, Double.MIN_VALUE};
		gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, 0.0, 0.0, Double.MIN_VALUE};
		contentPanel.setLayout(gbl_contentPanel);
				{
					lblId = new JLabel("姓名");
					GridBagConstraints gbc_lblId = new GridBagConstraints();
					gbc_lblId.fill = GridBagConstraints.BOTH;
					gbc_lblId.insets = new Insets(0, 0, 5, 0);
					gbc_lblId.gridwidth = 4;
					gbc_lblId.gridx = 0;
					gbc_lblId.gridy = 0;
					contentPanel.add(lblId, gbc_lblId);
				}
		
				lblClass = new JLabel("班级");
				GridBagConstraints gbc_lblClass = new GridBagConstraints();
				gbc_lblClass.anchor = GridBagConstraints.NORTH;
				gbc_lblClass.fill = GridBagConstraints.HORIZONTAL;
				gbc_lblClass.insets = new Insets(0, 0, 5, 5);
				gbc_lblClass.gridwidth = 2;
				gbc_lblClass.gridx = 0;
				gbc_lblClass.gridy = 1;
				contentPanel.add(lblClass, gbc_lblClass);
				
						lblRole = new JLabel("权限");
						GridBagConstraints gbc_lblRole = new GridBagConstraints();
						gbc_lblRole.anchor = GridBagConstraints.NORTH;
						gbc_lblRole.fill = GridBagConstraints.HORIZONTAL;
						gbc_lblRole.insets = new Insets(0, 0, 5, 5);
						gbc_lblRole.gridx = 2;
						gbc_lblRole.gridy = 1;
						contentPanel.add(lblRole, gbc_lblRole);
				{
					JLabel lblNick = new JLabel("昵称");
					GridBagConstraints gbc_lblNick = new GridBagConstraints();
					gbc_lblNick.fill = GridBagConstraints.HORIZONTAL;
					gbc_lblNick.insets = new Insets(0, 0, 5, 5);
					gbc_lblNick.gridx = 0;
					gbc_lblNick.gridy = 2;
					contentPanel.add(lblNick, gbc_lblNick);
				}
				{
					textField = new JTextField();
					GridBagConstraints gbc_textField = new GridBagConstraints();
					gbc_textField.anchor = GridBagConstraints.NORTH;
					gbc_textField.fill = GridBagConstraints.HORIZONTAL;
					gbc_textField.insets = new Insets(0, 0, 5, 5);
					gbc_textField.gridx = 1;
					gbc_textField.gridy = 2;
					contentPanel.add(textField, gbc_textField);
					textField.setColumns(10);
				}
						
								JLabel lblNewLabel = new JLabel("描述");
								GridBagConstraints gbc_lblNewLabel = new GridBagConstraints();
								gbc_lblNewLabel.anchor = GridBagConstraints.WEST;
								gbc_lblNewLabel.insets = new Insets(0, 0, 5, 5);
								gbc_lblNewLabel.gridx = 2;
								gbc_lblNewLabel.gridy = 2;
								contentPanel.add(lblNewLabel, gbc_lblNewLabel);
				
						tfDesc = new JTextField();
						GridBagConstraints gbc_tfDesc = new GridBagConstraints();
						gbc_tfDesc.fill = GridBagConstraints.HORIZONTAL;
						gbc_tfDesc.anchor = GridBagConstraints.NORTH;
						gbc_tfDesc.insets = new Insets(0, 0, 5, 0);
						gbc_tfDesc.gridx = 3;
						gbc_tfDesc.gridy = 2;
						contentPanel.add(tfDesc, gbc_tfDesc);
						
		
		lblCourseInfo = new JLabel("New label");
		GridBagConstraints gbc_lblCourseInfo = new GridBagConstraints();
		gbc_lblCourseInfo.anchor = GridBagConstraints.NORTH;
		gbc_lblCourseInfo.fill = GridBagConstraints.HORIZONTAL;
		gbc_lblCourseInfo.gridwidth = 4;
		gbc_lblCourseInfo.gridx = 0;
		gbc_lblCourseInfo.gridy = 3;
		contentPanel.add(lblCourseInfo, gbc_lblCourseInfo);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setBackground(new Color(176, 196, 222));
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);

			JButton btnChangepsw = new JButton("修改密码");
			btnChangepsw.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					GUI.showModalDialog(new Dlg_ChangePsw(Dlg_UserDetail.this.userManager));
				}
			});
			buttonPane.add(btnChangepsw);
			{
				JButton okButton = new JButton("确定");
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent e) {
						saveChange();
						Dlg_UserDetail.this.dispose();
						
					}
				});
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
		}
		updateValue();
	}
}
